package main

import "fmt"

//#link: https://leetcode-cn.com/problems/continuous-subarray-sum/
func main() {
	nums := []int{23, 2, 4, 6, 7}
	k := 6
	fmt.Println(checkSubarraySum(nums, k))
}

//利用前缀和加哈希 前缀和代表余数
func checkSubarraySum(nums []int, k int) bool {
	tempMap := make(map[int]int)
	tempMap[0] = -1
	preSum := 0
	for i, num := range nums {
		preSum = (preSum + num) % k
		if value, ok := tempMap[preSum]; ok {
			if i-value >= 2 {
				return true
			}
		} else {
			tempMap[preSum] = i
		}

	}
	return false
}
